﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using COM.Rights.Model;
using COM.Rights.BLL;
using System.Data;
using COM.PreAlarm.Model;
using COM.PreAlarm.BLL;
using System.Web.UI.WebControls;

public partial class Pages_ProductTotal_SendMsg : AnalyticsListBase
{  
	#region 重写方法

    
     protected string _FusionCartXML = "";

    protected override void OnLoad()
    {
        
        if (!IsPostBack) {
          
            //初始化市
            MdlistInfo _MdlistInfo = new MdlistInfo();
            _MdlistInfo.TranType = "GetCityByUser";
            _MdlistInfo.UID = ZASession.Account;

            DataTable dt = Mdlist.GetDataTableByModel(_MdlistInfo);
            if (dt != null)
            {
                if (dt.Rows.Count == 1)
                {
                    //一条记录默认选中
                    ZABinder.FillList(StringHelper.FormatObjToString(dt.Rows[0]["Name"]), ddlCity, false, dt);
                    //填充区
                    _MdlistInfo.TranType = "GetDistrictByUser";
                    _MdlistInfo.UID = ZASession.Account;
                    _MdlistInfo.Type = ddlCity.SelectedValue;
                    DataTable dtDistrict = Mdlist.GetDataTableByModel(_MdlistInfo);
                    //一条记录默认选中
                    if (dtDistrict != null && dtDistrict.Rows.Count == 1)
                    {
                        ZABinder.FillList(StringHelper.FormatObjToString(dtDistrict.Rows[0]["name"]), ddlDistrict, false, dtDistrict);
                    }
                    else
                    {
                        ZABinder.FillList(string.Empty, ddlDistrict, true, dtDistrict);
                    }

                }
                else
                {
                    ZABinder.FillList(string.Empty, ddlCity, true, dt);
                    ddlDistrict.Items.Add(new ListItem("--请选择--", ""));
                }

            }
            else
            {
                //没有市的权限，显示所管控的所有区
                ZABinder.FillList(string.Empty, ddlCity, true, dt);
                ddlDistrict.Items.Add(new ListItem("--请选择--", ""));
            }

            _MdlistInfo.TranType = "GetVersion";
            ZABinder.FillList(string.Empty, ddlDistrict, true,  Mdlist.GetDataTableByModel(_MdlistInfo));
        }

    
        base.OnLoad();
    }




    protected override void OnGridDataBind()
    {
        BdOrder _BdOrder = new BdOrder();
        List<DBParameter> _listDBParameter = new List<DBParameter>();
        _listDBParameter.Add(new DBParameter("@TranType", "SendMore"));
        //listDBParameter.Add(new DBParameter("@OrderDateBegin", fromData.Text));
        //_listDBParameter.Add(new DBParameter("@OrderDateEnd", toData.Text));
        _listDBParameter.Add(new DBParameter("@UID", ZASession.Account));
        _listDBParameter.Add(new DBParameter("@City", ddlCity.SelectedValue));
        _listDBParameter.Add(new DBParameter("@District", ddlDistrict.SelectedValue));
        _listDBParameter.Add(new DBParameter("@Version", ddlVersion.SelectedValue));
       // _listDBParameter.Add(new DBParameter("@Equip_NO", Equip_No.Text));
       // _listDBParameter.Add(new DBParameter("@AvgTotalBegin", StringHelper.FormatObjStringToint(txtAvgBegin.Text)));
       // _listDBParameter.Add(new DBParameter("@AvgTotalEnd", StringHelper.FormatObjStringToint(txtAvgEnd.Text)));

        IDataReader sdr = _BdOrder.ExecuteReader("[dbo].[report]", _listDBParameter, System.Data.CommandType.StoredProcedure);
  

        DataTable dt = new DataTable();
        dt.Load(sdr);
        dt.Columns.Add("CurrentStatus");
        string TargetDate = DateTime.Now.Hour.ToString().Length < 2 ? '0' + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString();
        foreach (DataRow dr in dt.Rows)
        {
            dr["CurrentStatus"] = StringHelper.FormatObjStringToint(dr["online_" + TargetDate]) == 1 ? "在线" : "离线";
        }


        DataView dv = dt.DefaultView;

        if (ddlMsgStatus.SelectedIndex != -1 && ddlMsgStatus.SelectedValue != "")
        {
            dv.RowFilter = string.Format("CurrentStatus='{0}' ", ddlMsgStatus.SelectedValue);
        }
        DataTable dt2 = dv.ToTable();

        BaseMainPager.RecordCount = dt2.Rows.Count;
        //gvInfos.DataSource = ZPager.TablePage(dt2, BaseMainPager.PageSize, BaseMainPager.CurrentPageIndex);
        gvInfos.DataSource = ZPager.TablePage(dt2, 100000, BaseMainPager.CurrentPageIndex);
        BaseMainPager.Visible = false;
        gvInfos.DataKeyNames = new string[] { "Equip_No" };//主键
        gvInfos.DataBind();
    }

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (ddlCity.SelectedValue != string.Empty)
        {
            //初始化区
            MdlistInfo _MdlistInfo = new MdlistInfo();
            _MdlistInfo.TranType = "GetDistrictByUser";
            _MdlistInfo.UID = ZASession.Account;
            _MdlistInfo.Type = ddlCity.SelectedValue;
            ZABinder.FillList(string.Empty, ddlDistrict, true, Mdlist.GetDataTableByModel(_MdlistInfo));
        }

    }

    protected virtual List<string> GetCheckedList2()
    {
        List<string> checkedList = new List<string>();

        for (int i = 0; i < BaseMainGrid.Rows.Count; i++)
        {
            if (BaseMainGrid.Rows[i].RowType == DataControlRowType.DataRow)
            {
                if (BaseMainGrid.Rows[i].FindControl("cbItem") != null)
                {
                    Control ck = BaseMainGrid.Rows[i].FindControl("cbItem");
                  
                    if (ck is CheckBox)
                    {
                        CheckBox cb = ck as CheckBox;
                        if (cb.Checked) {
                            Control hidID = BaseMainGrid.Rows[i].FindControl("hidID");
                            checkedList.Add((hidID as HiddenField).Value);
                        }
                           
                        //ForeachGridDataRow(cb.Checked, BaseMainGrid.Rows[i], i);
                    }
                }
            }
            ForeachGridRow(BaseMainGrid.Rows[i]);
        }
        return checkedList;
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        //SendSetInformation();
        //if (!string.IsNullOrEmpty(txtMessage.Text.Trim()))
        //{
        //    List<string> checkedList = GetCheckedList2();
        //    if (checkedList != null && checkedList.Count > 0)
        //    {
        //        SetMsg_SentInfo msi = new SetMsg_SentInfo();
        //        msi.SetMsg_No = string.Format("HNXXFS_{0}", DateTime.Now.ToString("yyyyMMddHHmmss"));
        //        msi.SetMsg_Content =  txtMessage.Text;//ddlTimes.SelectedValue +
        //        SetMsg_Sent msg = new SetMsg_Sent();
        //        msg.Add(msi, "*");
        //        Equip_SetMsg ems = new COM.PreAlarm.BLL.Equip_SetMsg();
        //        foreach (string s in checkedList)
        //        {
        //            Equip_SetMsgInfo emsi = new COM.PreAlarm.Model.Equip_SetMsgInfo();
        //            emsi.SetIsNew(true);
        //            emsi.Equip_No = s;
        //            emsi.SetMsg_No = msi.SetMsg_No;
        //            emsi.SetMsg_Time = StringHelper.FormatStringToDate(txtSendDate.Value);
        //            emsi.SetMsg_RecStatus = 0;
        //            ems.Add(emsi, "*");

        //        }
        //        ClientScript.RegisterStartupScript(GetType(), "Success", "<script>alert('发送成功！');</script>");
        //        //ems.Add(
        //    }
        //    else
        //    {
        //        ClientScript.RegisterStartupScript(GetType(), "Error", "<script>alert('请选择要发送的设备！');</script>");
        //    }
        //}
        //else
        //{
        //    ClientScript.RegisterStartupScript(GetType(), "Error", "<script>alert('请录入要发送的信息！');</script>");
        //    txtMessage.Focus();
        //}

    }


    protected string GetTextByValue(string Value, string type)
    {
        return Mdlist.GetTextByValue(Value, type);
    }

	#endregion 	重写方法

    //处理设置信息
    private string GetSetInfor(string str)
    {

        return "";
 
    }

    ////设置管理员号码
    //protected void btnSimSet_Click(object sender, EventArgs e)
    //{
    //    if (txtA1.Text.Trim() != "")
    //    {
    //        string simA = txtA1.Text.Trim();
    //        simA += "12345678S 02 A1" + simA + "F";
    //    }

    //}
   

    //发送不同的设置信息
    private void SendSetInformation( string sendMsg)
    {
        if (sendMsg.Length>13)
        {
            List<string> checkedList = GetCheckedList2();
            if (checkedList != null && checkedList.Count > 0)
            {
                SetMsg_SentInfo msi = new SetMsg_SentInfo();
                msi.SetMsg_No = string.Format("HNXXFS_{0}", DateTime.Now.ToString("yyyyMMddHHmmss"));
                msi.SetMsg_Content = sendMsg;//ddlTimes.SelectedValue +
                SetMsg_Sent msg = new SetMsg_Sent();
                msg.Add(msi, "*");
                Equip_SetMsg ems = new COM.PreAlarm.BLL.Equip_SetMsg();
                foreach (string s in checkedList)
                {
                    Equip_SetMsgInfo emsi = new COM.PreAlarm.Model.Equip_SetMsgInfo();
                    emsi.SetIsNew(true);
                    emsi.Equip_No = s;
                    emsi.SetMsg_No = msi.SetMsg_No;
                    emsi.SetMsg_Time = StringHelper.FormatStringToDate(txtSendDate.Value);
                    emsi.SetMsg_RecStatus = 0;
                    ems.Add(emsi, "*");

                }
                ClientScript.RegisterStartupScript(GetType(), "Success", "<script>alert('发送成功！');</script>");
                //ems.Add(
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "Error", "<script>alert('请选择要发送的设备！');</script>");
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "Error", "<script>alert('请录入正确的的信息！');</script>");
            //txtMessage.Focus();
        }
 
    }

    protected void btnSetSim_Click(object sender, EventArgs e)
    {
        string select = this.selSim.Value;
        string header =  "12345678S 02 ";
        string setInfo="";
        string msgSend = "";
        int count = 0;
        if (select == "1")
        {
           
            if (!string.IsNullOrEmpty(txtA1.Text.Trim()))
            {
                setInfo +=  "86" + txtA1.Text.Trim() + "F";
            }
            if (!string.IsNullOrEmpty(txtA2.Text.Trim()))
            {
                setInfo +=  "86" + txtA2.Text.Trim() + "F";
            }
            if (!string.IsNullOrEmpty(txtA3.Text.Trim()))
            {
                setInfo +=  "86" + txtA3.Text.Trim() + "F";
            }
            if (!string.IsNullOrEmpty(txtA4.Text.Trim()))
            {
                setInfo +=  "86" + txtA4.Text.Trim() + "F";
            }
            count = setInfo.Length / 14;
            
            if (count != 0)
            {
                msgSend = header + "A" + count + setInfo;

            }
           
            SendSetInformation(msgSend);
        }
        if (select == "2")
        {
            if (!string.IsNullOrEmpty(txtA1.Text.Trim()))
            {
                setInfo += txtA1.Text.Trim() + "F";
            }
            if (!string.IsNullOrEmpty(txtA2.Text.Trim()))
            {
                setInfo +=  txtA2.Text.Trim() + "F";
            }
            if (!string.IsNullOrEmpty(txtA3.Text.Trim()))
            {
                setInfo +=  txtA3.Text.Trim() + "F";
            }
            if (!string.IsNullOrEmpty(txtA4.Text.Trim()))
            {
                setInfo +=  txtA4.Text.Trim() + "F";
            }

            char[] charSetInfo = setInfo.ToCharArray();
            for (int i = 0; i < charSetInfo.Length; i++)
            {
                if (charSetInfo[i] == 'F')
                {
                    count++;
                }
 
            }
            if (count != 0)
            {
                msgSend = header + "A" + count + setInfo;
               
            }
            SendSetInformation(msgSend);
            
        }
    }
    //删除全部的管理员号码
    protected void btnDetSim_Click(object sender, EventArgs e)
    {
        string header = "12345678S 02 DA";
        SendSetInformation(header);
    }
    //设置短信中心号码
    protected void btnSetMsgCenter_Click(object sender, EventArgs e)
    {
        string setMsg = "";
        if (!string.IsNullOrEmpty(txtMsgCenter.Text.Trim()))
        {
            setMsg = "12345678S 04 86" + txtMsgCenter.Text.Trim();
            
        }
        SendSetInformation(setMsg);
    }
    //设置时间
    protected void btnTimeSet_Click(object sender, EventArgs e)
    {
         DateTime dt = DateTime.Now;
         string strTime ="12345678S 08 "+ dt.Year.ToString().Substring(2, 2) + "/" + getMonthDay(dt.Month) + "/" + getMonthDay(dt.Day) + "," + dt.ToString().Split(' ')[1];
         SendSetInformation(strTime);

    }
    //系统重启
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string msgReset = "12345678S 07      ";
        SendSetInformation(msgReset);
    }
    //远程升级
    protected void Button1_Click(object sender, EventArgs e)
    {
        string msgReset = "12345678U FLASH_UPGRADE";
        SendSetInformation(msgReset);
    }
    //处理时间
    private string getMonthDay(int i)
    {
        string md = "";
        if (i < 10)
        {
            md = "0" + i;
        }
        else
        {
            md = i.ToString();
        }
        return md;
    }

    
}